package net.lab1024.sa.admin.module.business.ball.util;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class BallUtil {

    /**
     * 给定一个List，并定义最小、最大值，统计list中各个数字出现的次数，并按照出现次数从小到大排序，包含0次
     * @param list
     * @param minNum
     * @param maxNum
     * @return
     */
    public static List<Integer> countAndSort(List<Integer> list,int minNum, int maxNum) {
        Map<Integer, Integer> countMap = new HashMap<>();
        IntStream.rangeClosed(minNum, maxNum)
                .forEach(num -> countMap.put(num, 0));
        list.forEach(num -> countMap.put(num, countMap.get(num) + 1));
        List<Integer> sortedNumbers = countMap.keySet().stream()
                .sorted(Comparator.comparingInt(countMap::get))
                .collect(Collectors.toList());
        return sortedNumbers;
    }
}
